package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.admin.constant.UserConstant;
import com.shop.admin.dao.ShopUserDao;
import com.shop.admin.entity.User;
import com.shop.admin.service.ShopUserService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.vo.UserStatusVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ShopUserServiceImpl extends ServiceImpl<ShopUserDao, User> implements ShopUserService {
    @Autowired
    private ShopUserDao userDao;
    @Override
    public User selectById(Integer userId) {
        User user = this.baseMapper.selectById(userId);
        return user;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Page<User> page = new Page<>(Long.parseLong(params.get("page").toString()),Long.valueOf(params.get("limit").toString()) );
        Page<User> resPage=userDao.pageUserDesc(page,params);
        PageUtils pageUtils = new PageUtils(resPage);
        return pageUtils;
    }

    @Override
    public void updateStatus(UserStatusVo vo) {
        User user=new User();
        if (vo.getStatus()==2){
            user.setStatus(UserConstant.StatusEnum.STOP.getCode());
            this.baseMapper.update(user,new QueryWrapper<User>().lambda().eq(User::getUserId,vo.getUserId()));
        }
        if (vo.getStatus()==1){
            user.setStatus(UserConstant.StatusEnum.NORMAL.getCode());
            this.baseMapper.update(user,new QueryWrapper<User>().lambda().eq(User::getUserId,vo.getUserId()));
        }
    }
}
